package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.widget.FlowLayout;
import com.itboye.eweilisten.util.widget.MyListView;

import java.util.List;

/**
 * 完形填空
 */
public class WanXingAdapter extends RecyclerView.Adapter<WanXingAdapter.MyViewHolder> {
    Context context;
    private List<TIbean> beans;
    WanXingTwoAdapter adapter;
    List<TIbean.Clild> list;
    public WanXingAdapter(Context context, List<TIbean> beans) {
        this.context = context;
        this.beans = beans;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_wanxing,null,false);
        MyViewHolder  holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        for (int i = 0; i < beans.get(position).getContent().size(); i++) {
            questionContentAdd(holder.questionLine, beans.get(position).getContent().get(i),i);
        }
        list=beans.get(position).getClild();
        if (adapter == null) {
            adapter=new WanXingTwoAdapter(context,list);
            holder.answerLine.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }
    /**
     * 内容
     * @param questionLine
     * @param string
     */
    private void questionContentAdd(final FlowLayout questionLine, String string,int i) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 0, 5);
        if (string.contains("_")) {
            TextView textView = new TextView(context);
            textView.setText(Html.fromHtml("<u>"+"("+string.replace("_","")+")"+"</u>"));
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setPadding(15, 0, 15, 0);
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        } else {
            TextView textView = new TextView(context);
            textView.setText(string);
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        }
    }
    @Override
    public int getItemCount() {
        return beans.size();
    }



    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public FlowLayout questionLine;
        public MyListView answerLine;
        public TextView timu_num;

        public MyViewHolder(View itemView) {
            super(itemView);
            questionLine = (FlowLayout) itemView.findViewById(R.id.questionLine);
            answerLine = (MyListView) itemView.findViewById(R.id.answerLine);
            timu_num = (TextView) itemView.findViewById(R.id.timu_num);
        }
    }
}
